<?php

class EmployeeResearchPublicationForm extends sfForm
{

    private $employeeService;
    public $employeeResearchPublications;

    /**
     * Get EmployeeService
     * @returns EmployeeService
     */
    public function getEmployeeService()
    {
        if (is_null($this->employeeService)) {
            $this->employeeService = new EmployeeService();
            $this->employeeService->setEmployeeDao(new EmployeeDao());
        }
        return $this->employeeService;
    }

    /**
     * Set EmployeeService
     * @param EmployeeService $employeeService
     */
    public function setEmployeeService(EmployeeService $employeeService)
    {
        $this->employeeService = $employeeService;
    }

    public function configure()
    {
        $this->employeeResearchPublicationPermissions = $this->getOption('employeeResearchPublicationPermissions');

        $empNumber = $this->getOption('empNumber');

        $widgets = array('emp_number' => new sfWidgetFormInputHidden(array(), array('value' => $empNumber)));
        $validators = array('emp_number' => new sfValidatorString(array('required' => false)));

        if ($this->employeeResearchPublicationPermissions->canRead()) {

            $employeeResearchPublicationWidgets = $this->getEmployeeResearchPublicationWidgets();
            $employeeResearchPublicationValidators = $this->getEmployeeResearchPublicationValidators();

            if (!($this->employeeResearchPublicationPermissions->canUpdate() || $this->employeeResearchPublicationPermissions->canCreate())) {
                foreach ($employeeResearchPublicationWidgets as $widgetName => $widget) {
                    $widget->setAttribute('disabled', 'disabled');
                }
            }
            $widgets = array_merge($widgets, $employeeResearchPublicationWidgets);
            $validators = array_merge($validators, $employeeResearchPublicationValidators);
        }

        $this->setWidgets($widgets);
        $this->setValidators($validators);

        $this->employeeResearchPublications = $this->getEmployeeService()->getEmployeeResearchPublicationRecords($empNumber);
        $this->widgetSchema->setNameFormat('employeeResearchPublication[%s]');
    }

    /*
     * Tis fuction will return the widgets of the form
     */

    public function getEmployeeResearchPublicationWidgets()
    {
        $widgets = array();

        //creating widgets
        $widgets['seqno'] = new sfWidgetFormInputHidden();
        $widgets['title'] = new sfWidgetFormInputText();
        $widgets['authors'] = new sfWidgetFormInputText();
        $widgets['publisher'] = new sfWidgetFormInputText();
        $widgets['publishing_year'] = new sfWidgetFormInputText();

        return $widgets;
    }

    /*
     * Tis fuction will return the form validators
     */

    public function getEmployeeResearchPublicationValidators()
    {
        $validators = array(
            'seqno' => new sfValidatorString(array('required' => false)),
            'title' => new sfValidatorString(array('required' => true, 'trim' => true, 'max_length' => 255)),
            'authors' => new sfValidatorString(array('required' => true, 'trim' => true, 'max_length' => 150)),
            'publisher' => new sfValidatorString(array('required' => true, 'trim' => true, 'max_length' => 150)),
            'publishing_year' => new sfValidatorString(array('required' => false, 'trim' => true, 'max_length' => 4)),
        );

        return $validators;
    }

}

?>